package rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.packageableElement.dependency;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.uml2.common.edit.command.SubsetSupersetReplaceCommand;
import org.eclipse.uml2.common.edit.command.SubsetSupersetSetCommand;
import org.eclipse.uml2.common.edit.command.SupersetRemoveCommand;
import org.eclipse.uml2.uml.ComponentRealization;
import org.eclipse.uml2.uml.UMLPackage;

import rad.framework.uml2.uml.edit.UMLEditPlugin;

public class ComponentRealizationItemProvider
		extends RealizationItemProvider
		implements IEditingDomainItemProvider, IStructuredItemContentProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource {

	public ComponentRealizationItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	@Override
	public List getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addAbstractionPropertyDescriptor(object);
			addRealizingClassifierPropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	protected void addAbstractionPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_ComponentRealization_abstraction_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_ComponentRealization_abstraction_feature", "_UI_ComponentRealization_type"),
				UMLPackage.Literals.COMPONENT_REALIZATION__ABSTRACTION, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addRealizingClassifierPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_ComponentRealization_realizingClassifier_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_ComponentRealization_realizingClassifier_feature", "_UI_ComponentRealization_type"),
				UMLPackage.Literals.COMPONENT_REALIZATION__REALIZING_CLASSIFIER, true, null, null, null));
	}

	@Override
	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/ComponentRealization"));
	}

	@Override
	public String getText(Object object) {
		final String label = ((ComponentRealization) object).getName();
		return label == null || label.length() == 0 ? getString("_UI_ComponentRealization_type") :
				getString("_UI_ComponentRealization_type") + " " + label;
	}

	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(ComponentRealization.class)) {
			case UMLPackage.COMPONENT_REALIZATION__REALIZING_CLASSIFIER:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
		}
		super.notifyChanged(notification);
	}

	@Override
	protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);
	}

	@Override
	public ResourceLocator getResourceLocator() {
		return UMLEditPlugin.INSTANCE;
	}

	@Override
	protected Command createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection) {
		if (feature == UMLPackage.Literals.DEPENDENCY__CLIENT) {
			return new SupersetRemoveCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.COMPONENT_REALIZATION__ABSTRACTION }, collection);
		}
		if (feature == UMLPackage.Literals.DEPENDENCY__SUPPLIER) {
			return new SupersetRemoveCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.COMPONENT_REALIZATION__REALIZING_CLASSIFIER }, collection);
		}
		return super.createRemoveCommand(domain, owner, feature, collection);
	}

	@Override
	protected Command createReplaceCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, EObject value, Collection collection) {
		if (feature == UMLPackage.Literals.DEPENDENCY__CLIENT) {
			return new SubsetSupersetReplaceCommand(domain, owner, feature, null, new EStructuralFeature[] { UMLPackage.Literals.COMPONENT_REALIZATION__ABSTRACTION }, value, collection);
		}
		if (feature == UMLPackage.Literals.DEPENDENCY__SUPPLIER) {
			return new SubsetSupersetReplaceCommand(domain, owner, feature, null, new EStructuralFeature[] { UMLPackage.Literals.COMPONENT_REALIZATION__REALIZING_CLASSIFIER }, value, collection);
		}
		return super.createReplaceCommand(domain, owner, feature, value, collection);
	}

	@Override
	protected Command createSetCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value) {
		if (feature == UMLPackage.Literals.COMPONENT_REALIZATION__ABSTRACTION) {
			return new SubsetSupersetSetCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.DEPENDENCY__CLIENT }, null, value);
		}
		if (feature == UMLPackage.Literals.COMPONENT_REALIZATION__REALIZING_CLASSIFIER) {
			return new SubsetSupersetSetCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.DEPENDENCY__SUPPLIER }, null, value);
		}
		return super.createSetCommand(domain, owner, feature, value);
	}

}
